#import <UIKit/UIKit.h>

#import "FWApplication.h"
#import "FWDetailListSection.h"
#import "FWDetailListEntry.h"

extern CGFloat const kFWDetailListViewSystemFontSize;
extern CGFloat const kFWDetailListViewLabelSize;
extern CGFloat const kFWDetailListViewExtraHeaderSpace;



@protocol FWDetailListViewControl <NSObject>

- (void)removeItem;
- (void)displayPopup:(NSString*)message;
- (void)displayPopupInstruction:(NSString*)message;

@end


@protocol FWDetailListViewDelegate <NSObject>

//- (NSArray*)dataForIndex:(NSInteger)i list:(NSArray*)list database:(NSString*)dbpath application:(FWApplication*)application;
- (NSArray*)dataForDetailListPage:(NSObject*)page key:(NSObject*)key application:(FWApplication*)application;


@optional

@property (assign) id<FWDetailListViewControl> controlDelegate;

- (NSArray*)toolbarForDetailListPage:(NSObject*)page key:(NSObject*)key application:(FWApplication*)application;

- (void)tableReordered:(NSArray*)sections;
- (BOOL)shouldReloadWhenTableReordered:(NSArray*)sections;


- (NSInteger)targetIndexForMove:(FWDetailListEntry*)entry toSection:(FWDetailListSection*)section proposedIndexPath:(NSIndexPath*)proposedIndexPath;
- (void)movedEntry:(FWDetailListEntry*)entry sections:(NSArray*)sections fromIndexPath:(NSIndexPath*)fromIndexPath toIndexPath:(NSIndexPath*)toIndexPath;
- (BOOL)allowDelete:(FWDetailListEntry*)entry indexPath:(NSIndexPath*)proposedIndexPath;
- (void)deletedEntry:(FWDetailListEntry*)entry sections:(NSArray*)sections indexPath:(NSIndexPath*)fromIndexPath;


- (BOOL)cancelClicked:(FWApplication*)application sections:(NSArray*)sections;
- (BOOL)doneClicked:(FWApplication*)application sections:(NSArray*)sections;
- (BOOL)selectClicked:(FWApplication*)application sections:(NSArray*)sections;
- (BOOL)buttonClicked:(FWApplication*)application sections:(NSArray*)sections button:(FWDetailListEntry*)button;

- (void)switchChanged:(FWApplication*)application sections:(NSArray*)sections;

- (NSArray*)listPageInfoForKey:(NSObject*)key currentPage:(NSObject*)page displayDepth:(NSInteger)depth application:(FWApplication*)application;
- (NSString*)captionForListPage:(NSObject*)page forKey:(NSObject*)key displayDepth:(NSInteger)depth application:(FWApplication*)application;
- (NSString*)listPageTextForKey:(NSObject*)key currentPage:(NSObject*)page displayDepth:(NSInteger)depth application:(FWApplication*)application;
- (BOOL)listPageTextScrollToEndForKey:(NSObject*)key currentPage:(NSObject*)page displayDepth:(NSInteger)depth application:(FWApplication*)application;
- (UIView*)listPageViewForKey:(NSObject*)key currentPage:(NSObject*)page displayDepth:(NSInteger)depth rect:(CGRect)rect application:(FWApplication*)application;
- (NSString*)listPagePopupForKey:(NSObject*)key currentPage:(NSObject*)page displayDepth:(NSInteger)depth application:(FWApplication*)application;

- (void)willShowBasePage:(NSObject*)page;

- (NSString*)doneButtonCaptionForKey:(NSObject*)key currentPage:(NSObject*)page displayDepth:(NSInteger)depth application:(FWApplication*)application;

@end


@interface FWDetailListView : UIView <UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate, FWDetailListViewControl>
{
    NSInteger index;
    NSArray* sections;
    NSString* caption;
    NSMutableArray* linkStack;
    
    BOOL lastIndexWasEarlier;
}


@property (nonatomic, retain) id<FWDetailListViewDelegate> delegate;
@property (retain) FWApplication* application;
@property (assign) NSInteger index;
@property (nonatomic, retain) NSArray* list;
@property (nonatomic, retain) NSObject* listKey;
@property (nonatomic, retain) NSString* caption;
@property (retain) UIView* listView;
@property (retain) UITableView* tableView;
@property (retain) UITextView* textView;
@property (retain) UIView* accessoryView;
@property (retain) UISegmentedControl* navControl;
@property (retain) UIToolbar* toolbar;
@property (retain) UINavigationBar* navBar;
@property (nonatomic, retain) NSArray* sections;
@property (retain) NSMutableArray* linkStack;
@property (retain) UIColor* detailTextColor;

@property (assign) CGFloat reloadViewOffset;
@property (assign) BOOL reloadViewAnimated;


- (id)initWithFrame:(CGRect)r list:(NSArray*)l index:(NSInteger)i application:(FWApplication*)app delegate:(id<FWDetailListViewDelegate>)d;
- (id)initWithFrame:(CGRect)r list:(NSArray*)l index:(NSInteger)i caption:(NSString*)caption application:(FWApplication*)app delegate:(id<FWDetailListViewDelegate>)d;
- (void)dealloc;

- (void)buildPreview:(CGRect)rect;

- (void)reloadData:(BOOL)animated;

- (FWDetailListSection*)tableView:(UITableView*)tableView sectionInfoForIndexPath:(NSIndexPath*)indexPath;
- (NSArray*)tableView:(UITableView*)tableView sectionContentsForIndexPath:(NSIndexPath*)indexPath;
- (FWDetailListEntry*)tableView:(UITableView*)tableView elementContentsForIndexPath:(NSIndexPath*)indexPath;

@end
